<template>
  <div class="full-box shadow-box padding-box">
    <OperateCommon :showBack="true" @back="goBack"></OperateCommon>
    <el-collapse v-model="activeNames" :accordion="false">
      <el-collapse-item
        v-for="item in items"
        :key="item.key"
        :title="item.title"
        :name="item.key"
      >
        <!-- 预警交办 -->
        <template v-if="item.key == '1'">
          <div class="desc">
            <label>预警项目：</label>
            <span>
              {{ detail.object_name }}
            </span>
          </div>
          <div class="desc">
            <label>建设单位：</label>
            <span>
              {{ detail.responsible_name }}
            </span>
          </div>
          <div class="desc">
            <label>预警单位：</label>
            <span>
              {{ detail.responsible_unit_name }}
            </span>
          </div>
          <div class="desc">
            <label>预警发生时间：</label>
            <span>
              {{ detail.trigger_time }}
            </span>
          </div>
          <div class="desc">
            <label>预警节点：</label>
            <span>
              {{ detail.scene }}
            </span>
          </div>
          <div class="desc">
            <label>预警内容：</label>
            <span>
              {{ detail.warning_content }}
            </span>
          </div>
          <div class="desc">
            <label>预警监控点：</label>
            <span>
              {{ detail.description }}
            </span>
          </div>
          <div class="desc">
            <label>承办单位：</label>
            <span>
              {{ detail.undertake_dept_name }}
            </span>
          </div>
          <div class="desc">
            <label>交办意见：</label>
            <span>
              {{ detail.assign_opinion }}
            </span>
          </div>
          <div class="desc">
            <label>附件材料：</label>
            <CommonUpload
              tableName="red-alert-assign"
              :useType="1"
              :pid="detail.assign_id"
              :showUploadBtn="false"
              :disabled="true"
            ></CommonUpload>
          </div>
          <div class="desc">
            <label>交办单位：</label>
            <span>
              {{ detail.assign_dept_name }}
            </span>
          </div>
          <div class="desc">
            <label>交办时间：</label>
            <span>
              {{ detail.assign_time }}
            </span>
          </div>
        </template>
        <!-- 预警接收 -->
        <template v-if="item.key == '2'">
          <!-- 反馈表单 -->
          <template v-if="detail.red_solve_status == 10">
            <template v-if="user.homeRole == 4">
              <el-form
                :model="acceptForm"
                ref="acceptForm"
                :rules="acceptRules"
                label-width="80px"
                :inline="false"
              >
                <!-- <el-form-item label="是否接收" prop="">
              <el-select v-model="acceptForm.status">
                <el-option :key="1" label="接收" :value="1"></el-option>
                <el-option :key="2" label="退回" :value="2"></el-option>
              </el-select>
            </el-form-item> -->
                <el-form-item label="接收意见" prop="feedback">
                  <el-input
                    type="textarea"
                    v-model="acceptForm.feedback"
                    placeholder=""
                    :autosize="{
                      minRows: 3,
                      maxRows: 6,
                    }"
                  ></el-input>
                </el-form-item>
                <el-form-item label="接收时间">
                  <el-date-picker
                    v-model="acceptForm.feedback_date"
                    type="date"
                    placeholder="选择日期时间"
                    disabled
                  >
                  </el-date-picker>
                </el-form-item>
                <el-form-item label="">
                  <el-button type="primary" @click="accpetProblem"
                    >接收</el-button
                  >
                  <el-button type="danger" @click="rejectProblem"
                    >退回</el-button
                  >
                </el-form-item>
              </el-form>
            </template>
            <template v-else>
              <div style="text-align: center">
                <b> 未反馈 </b>
              </div>
            </template>
          </template>
          <!-- 反馈结果 -->
          <template v-if="detail.red_solve_status != 10">
            <div class="desc">
              <label>接收状态：</label>
              <span>
                {{ detail.assign_status_text }}
              </span>
            </div>
            <div class="desc">
              <label>接收意见：</label>
              <span>
                {{ detail.feedback }}
              </span>
            </div>
            <div class="desc">
              <label>接收时间：</label>
              <span>
                {{ detail.feedback_date }}
              </span>
            </div>
          </template>
        </template>
        <!-- 处置信息 -->
        <template v-if="item.key == '3'">
          <!-- 处置表单 -->
          <!-- 案管室 -->
          <template v-if="user.homeRole == 3">
            <!-- 处置结果 -->
            <template
              v-if="
                detail.assignSolveDetail && detail.assignSolveDetail.status == 1
              "
            >
              <AlertPunishDetail
                :detail="detail"
                @transfer="continuePunish('transfer')"
                @rectification="continuePunish('rectification')"
                @oneCaseThreeCheck="continuePunish('oneCaseThreeCheck')"
              ></AlertPunishDetail>
            </template>
            <el-empty v-else :image-size="30" description="未处置"></el-empty>
          </template>

          <!-- 纪委组 -->
          <template v-if="user.homeRole == 4">
            <template
              v-if="
                !detail.assignSolveDetail ||
                (detail.assignSolveDetail &&
                  detail.assignSolveDetail.status == 0)
              "
            >
              <AlertPunish :detail="detail" @refresh="initData"></AlertPunish>
            </template>
            <!-- 处置结果 -->
            <template
              v-if="
                detail.assignSolveDetail && detail.assignSolveDetail.status == 1
              "
            >
              <AlertPunishDetail
                :detail="detail"
                @transfer="continuePunish('transfer')"
                @rectification="continuePunish('rectification')"
                @oneCaseThreeCheck="continuePunish('oneCaseThreeCheck')"
              ></AlertPunishDetail>
            </template>
          </template>
        </template>

        <!-- 问题移送 -->
        <template v-if="item.key == '4'">
          <template v-if="detail.warningTransferDetail">
            <QuestionTransferDetail :detail="detail"></QuestionTransferDetail>
          </template>
          <template v-else>
            <QuestionTransfer
              :detail="detail"
              @refresh="initData"
            ></QuestionTransfer>
          </template>
        </template>
        <!-- 整改 -->
        <template v-if="item.key == '5'">
          <template v-if="detail.warningMendDetail">
            <RectificationDetail :detail="detail"></RectificationDetail>
          </template>
          <template v-else>
            <Rectification :detail="detail" @refresh="initData"></Rectification>
          </template>
        </template>
        <!-- 一案三查 -->
        <template v-if="item.key == '6'">
          <template v-if="detail.warningCheckDetail">
            <OneCaseThreeCheckDetail :detail="detail"></OneCaseThreeCheckDetail>
          </template>
          <template v-else>
            <OneCaseThreeCheck
              :detail="detail"
              @refresh="initData"
            ></OneCaseThreeCheck>
          </template>
        </template>
      </el-collapse-item>
    </el-collapse>
  </div>
</template>

<script>
import {
  getRedAlerAssignDetail,
  postRedAlertFeedback,
} from "@/api/RedAlert/Index";
import CommonUpload from "@/components/Common/CommonUpload.vue";
import OperateCommon from "@/components/Common/OperateCommon.vue";
import LinkTo from "@/mixins/LinkTo";
import { getCurrentDateTime } from "@/utils/date";
import { required } from "@/utils/eValidate";
import AlertPunish from "./components/AlertPunish.vue";
import AlertPunishDetail from "./components/AlertPunishDetail.vue";
import OneCaseThreeCheck from "./components/OneCaseThreeCheck.vue";
import OneCaseThreeCheckDetail from "./components/OneCaseThreeCheckDetail.vue";
import QuestionTransfer from "./components/QuestionTransfer.vue";
import QuestionTransferDetail from "./components/QuestionTransferDetail.vue";
import Rectification from "./components/Rectification.vue";
import RectificationDetail from "./components/RectificationDetail.vue";

export default {
  name: "RedAlertDetails",
  mixins: [LinkTo],
  components: {
    CommonUpload,
    OperateCommon,
    AlertPunish,
    AlertPunishDetail,
    OneCaseThreeCheck,
    OneCaseThreeCheckDetail,
    QuestionTransfer,
    QuestionTransferDetail,
    Rectification,
    RectificationDetail,
  },
  data() {
    return {
      id: 0,
      pageType: 0,
      activeNames: ["1"],
      detail: {},
      items: [
        {
          key: "1",
          title: "预警交办信息",
        },
        // {
        //   key: "2",
        //   title: "交办反馈信息",
        // },
        // {
        //   key: "3",
        //   title: "处置信息",
        // },
        // {
        //   key: "4",
        //   title: "问题移送",
        // },
        // {
        //   key: "5",
        //   title: "整改",
        // },
        // {
        //   key: "6",
        //   title: "一案三查",
        // },
      ],
      acceptForm: {
        id: "", // 交办 ID
        status: 1, // 1 接收 2 退回
        status_text: "已反馈", // 已反馈 已退回
        feedback: "", // 接收意见
        feedback_date: getCurrentDateTime(), // 接收时间
      },
      acceptRules: {
        feedback: [required],
      },
      user: null,
    };
  },
  methods: {
    initData() {
      getRedAlerAssignDetail({ id: this.id }).then((res) => {
        if (res.code == 0) {
          this.detail = res.data;
          this.checkActiveNames(this.detail.red_solve_status);
          this.checkContinueStep();
        }
      });
    },
    goBack() {
      switch (parseInt(this.pageType)) {
        case 0:
          this.linkTo(`/redAlert/alertAssignList`);
          break;
        case 1:
          this.linkTo(`/redAlert/alertDisposeList`);
          break;
        case 3:
          this.linkTo(`/redAlert/manage`);
          break;
        default:
          break;
      }
    },
    accpetProblem() {
      this.$refs.acceptForm[0].validate((valid) => {
        if (valid) {
          this.acceptForm.id = this.detail.assign_id;
          this.acceptForm.status = 1;
          this.acceptForm.status_text = "已反馈";

          postRedAlertFeedback(this.acceptForm).then((res) => {
            if (res.code == 0) {
              this.$message.success("接收成功");
              this.initData();
            }
          });
        }
      });
    },
    rejectProblem() {
      this.$refs.acceptForm[0].validate((valid) => {
        if (valid) {
          this.acceptForm.id = this.detail.assign_id;
          this.acceptForm.status = 2;
          this.acceptForm.status_text = "已退回";

          postRedAlertFeedback(this.acceptForm).then((res) => {
            if (res.code == 0) {
              this.$message.success("退回成功");
              this.goBack();
            }
          });
        }
      });
    },
    checkActiveNames(status) {
      switch (parseInt(status)) {
        case 10:
          this.activeNames = ["1", "2"];
          break;
        case 15:
          this.activeNames = ["1", "2"];
          break;
        case 20:
          this.activeNames = ["1", "2", "3"];
          break;
      }
    },
    continuePunish(type) {
      switch (type) {
        case "transfer":
          if (
            this.items.findIndex((i) => {
              return i.key === "4";
            }) === -1
          ) {
            let newArr = JSON.parse(JSON.stringify(this.items));
            newArr.push({
              key: "4",
              title: "问题移送",
            });
            this.items = newArr;
          }
          this.activeNames = ["4"];
          break;
        case "rectification":
          if (
            this.items.findIndex((i) => {
              return i.key === "5";
            }) === -1
          ) {
            let newArr = JSON.parse(JSON.stringify(this.items));
            newArr.push({
              key: "5",
              title: "整改",
            });
            this.items = newArr;
          }
          this.activeNames = ["5"];
          break;
        case "oneCaseThreeCheck":
          if (
            this.items.findIndex((i) => {
              return i.key === "6";
            }) === -1
          ) {
            let newArr = JSON.parse(JSON.stringify(this.items));
            newArr.push({
              key: "6",
              title: "一案三查",
            });
            this.items = newArr;
          }
          this.activeNames = ["6"];
          break;
      }
    },
    checkContinueStep() {
      if (parseInt(this.detail.red_solve_status) >= 10) {
        if (
          this.items.findIndex((i) => {
            return i.key === "2";
          }) === -1
        ) {
          this.items.push({
            key: "2",
            title: "接收信息",
          });
        }
      }
      if (parseInt(this.detail.red_solve_status) >= 20) {
        if (
          this.items.findIndex((i) => {
            return i.key === "3";
          }) === -1
        ) {
          this.items.push({
            key: "3",
            title: "处置信息",
          });
        }
      }
      if (this.detail.warningTransferDetail) {
        if (
          this.items.findIndex((i) => {
            return i.key === "4";
          }) === -1
        ) {
          this.items.push({
            key: "4",
            title: "问题移送",
          });
        }
      }
      if (this.detail.warningMendDetail) {
        if (
          this.items.findIndex((i) => {
            return i.key === "5";
          }) === -1
        ) {
          this.items.push({
            key: "5",
            title: "整改",
          });
        }
      }
      if (this.detail.warningCheckDetail) {
        if (
          this.items.findIndex((i) => {
            return i.key === "6";
          }) === -1
        ) {
          this.items.push({
            key: "6",
            title: "一案三查",
          });
        }
      }
    },
  },
  created() {
    this.id = this.$route.params.id;
    this.pageType = this.$route.params.type;
    this.user = JSON.parse(localStorage.getItem("user"));

    this.initData();
  },
};
</script>

<style lang="scss" scoped>
</style>